﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BDCS.BLL;
using BDCS.Model;

namespace BDCS.UI
{
    public partial class AddSubBuildAttributes : Form
    {
        #region 成员字段
        private BDCS.BLL.T_SubBuildTyppes bllSubBuildType;
        private BDCS.BLL.T_SubBuildAttributes bllSubBuildAttributes;
        private Model.T_SubBuildAttributes modelSubBuildAttributes;
        private DataSet dataSet;
        private List<Model.T_SubBuildAttributes> listSubBuildAttributes;
        private bool isExist;
        #endregion
        #region 构造函数
        public AddSubBuildAttributes()
        {
            InitializeComponent();
        }
        #endregion
        #region 成员事件
        private void AddSubBuildAttributes_Load(object sender, EventArgs e)
        {
            dataSet = new DataSet();
            bllSubBuildType = new BLL.T_SubBuildTyppes();
            dataSet = bllSubBuildType.GetAllList();
            ComBoxLoadDataBinding(dataSet);
        }
       
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSubBuildAttributeName.Text.Trim()))
            {
                MessageBox.Show("请输入建筑属性名称！");
            }
            else
            {
                if (txtSubBuildAttributeName.Text.Length > 100)
                {
                    MessageBox.Show("输入的建筑属性名称长度大于100，请重新输入！");
                }
                else
                {
                    modelSubBuildAttributes = new Model.T_SubBuildAttributes();
                    modelSubBuildAttributes.SBTID = Convert.ToInt32(cmbSubBuildTypeName.SelectedValue);
                    modelSubBuildAttributes.SBAContent = txtSubBuildAttributeName.Text.Trim();
                    modelSubBuildAttributes.SBADescribe = txtSubBuildAttributeDescribe.Text.Trim();
                    if (!CheckAttributeIsExist(modelSubBuildAttributes))
                    {
                        if (bllSubBuildAttributes.Add(modelSubBuildAttributes) != 0)
                        {
                            BDCS.Common.TreeViewState.IsChangedAttribute = true;
                            MessageBox.Show("建筑属性名称添加成功！");
                        }
                        else
                        {
                            MessageBox.Show("建筑属性名称添加失败，请重新输入！！");
                            txtSubBuildAttributeName.Text = string.Empty;
                        }
                    }
                    else
                    {
                        MessageBox.Show("建筑属性名称已经存在，请重新输入！！");
                        txtSubBuildAttributeName.Text = string.Empty;
                    }
                }
            }
         }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtSubBuildAttributeName.Text = string.Empty;
            this.txtSubBuildAttributeDescribe.Text = string.Empty;
        } 
        #endregion
        #region 成员方法
        private void ComBoxLoadDataBinding(DataSet dataSet)
        {
            cmbSubBuildTypeName.DataSource = dataSet.Tables[0];
            cmbSubBuildTypeName.DisplayMember = dataSet.Tables[0].Columns["SBTName"].ToString();
            cmbSubBuildTypeName.ValueMember = dataSet.Tables[0].Columns["SBTID"].ToString();
        }
        private bool CheckAttributeIsExist(Model.T_SubBuildAttributes modelSubBuildAttribute)
        {
            listSubBuildAttributes = new List<Model.T_SubBuildAttributes>();
            bllSubBuildAttributes = new BLL.T_SubBuildAttributes();
            listSubBuildAttributes = bllSubBuildAttributes.GetModelList("SBTID=" + "'" + modelSubBuildAttribute.SBTID.ToString() + "'");
            if (listSubBuildAttributes.Count != 0)
            {
                foreach (Model.T_SubBuildAttributes SubBuildAttribute in listSubBuildAttributes)
                {
                    if ( modelSubBuildAttribute.SBTID == SubBuildAttribute.SBTID && modelSubBuildAttribute.SBAContent == SubBuildAttribute.SBAContent)
                    {
                        isExist = true;
                    }
                    else
                    {
                        isExist = false;
                    }
                }
            }
            else
            {
                isExist = false;
            }
            return isExist;
        } 
        #endregion
    }
}
